package com.baomidou.mybatisplus.core.conditions.segments;

import com.baomidou.mybatisplus.core.conditions.ISqlSegment;
import lombok.ToString;

import java.util.List;

import static com.baomidou.mybatisplus.core.enums.SqlKeyword.GROUP_BY;

/**
 * <p>GroupBySegmentList</p>
 *
 * <p>项目名称：fakerMybatisPlusUtil</p>
 *
 * <p>注释:无</p>
 *
 * <p>Copyright: Copyright Faker(c) 2023/1/20</p>
 *
 * <p>公司: Faker</p>
 *
 * @author 淡梦如烟
 * @version 1.0
 * @date 2023/1/20 22:27
 */
@SuppressWarnings("serial")
@ToString
public class GroupBySegmentList extends AbstractISegmentList {

    @Override
    protected boolean transformList(List<ISqlSegment> list, ISqlSegment firstSegment, ISqlSegment lastSegment) {
        list.remove(0);
        return true;
    }

    @Override
    protected String childrenSqlSegment() {
        if (isEmpty()) {
            return EMPTY;
        }
//        return this.stream().map(ISqlSegment::getSqlSegment).collect(joining(COMMA, SPACE + GROUP_BY.getSqlSegment() + SPACE, EMPTY));
        StringBuffer str = new StringBuffer();
        for (int i = 0; i < this.size(); i++) {
            ISqlSegment iSqlSegment = this.get(i);
            if (i == this.size() - 1) {
                str.append(SPACE + GROUP_BY.getSqlSegment() + SPACE).append(iSqlSegment.getSqlSegment()).append(SPACE);
            } else {
                str.append(SPACE + GROUP_BY.getSqlSegment() + SPACE).append(iSqlSegment.getSqlSegment()).append(SPACE).append(COMMA);
            }
        }
        return str.toString();
    }
}
